package com.note.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.note.domain.Note;
import com.note.mapper.NoteMapper;
import com.note.service.NoteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/6 0:19
 * @since JDK 1.8
 */
@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements NoteService {

    @Override
    public IPage<Note> notePageListByKeyWord(Integer page, Integer size, String keyword) {
        QueryWrapper<Note> noteQueryWrapper=new QueryWrapper<>();
        noteQueryWrapper.select("id","title","tag","createtime","viewnum");
        if(StringUtils.isNotBlank(keyword)){
            noteQueryWrapper.like("title",keyword);
            noteQueryWrapper.or().like("tag",keyword);
        }
        noteQueryWrapper.orderByDesc("createtime");
        IPage<Note> noteIPage=super.page(new Page(page,size),noteQueryWrapper);
        return noteIPage;
    }

    @Override
    public Integer addOneNoteReadCountByNid(Integer nid) {
        Integer code=0;
        Note note=super.getById(nid);
        if(note!=null){
            note.setViewnum(note.getViewnum()+1);
            code=super.getBaseMapper().updateById(note);
        }
        return code;
    }
}
